<?php


namespace app\admin\controller\mall;


use app\admin\model\MallGoods;
use app\admin\model\MallOrder;
use app\common\controller\AdminController;
use jianyan\excel\Excel;
use think\App;

class CountDetail extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallGoods();
    }
    /**
     * @NodeAnotation(title="用户拼团订单")
     */
    public function detail()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $userId  = MallOrder::where('id', input('id'))->value('user_id');
            $where[] = ['mall_order.user_id', '=', $userId];
            $where[] = ['order_status', 'in', [10, 30, 40, 50, 60]];
            $count   = MallOrder::withJoin(['user', 'goods', 'collage'], 'LEFT')
                ->where($where)
                ->count();
            $list    = MallOrder::withJoin(['user', 'goods', 'collage'], 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()
                ->toArray();
            $data    = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="导出")
     */
    public function export()
    {
        [$page, $limit, $where] = $this->buildTableParames();
        $header  = [
            ['订单号', 'order_no'],
            ['用户名', 'user_name'],
            ['商品名', 'goods_name'],
            ['订单状态', 'collage_status'],
            ['订单金额', 'order_price'],
            ['订单创建时间', 'create_time'],
            ['邀约人数', 'invite_num'],
            ['邀约金额', 'invite_price'],
            ['拼团发起时间', 'collage_create_time'],
        ];
        $where[] = ['order_status', 'in', [10, 30, 50, 60]];
        $where[] = ['user.is_virtual', '=', 0];
        $list    = MallOrder::withJoin(['user'], 'LEFT')
            ->where($where)
            ->group('user_id')
            ->field('count(1) as order_num,SUM(order_price) as order_price')
            ->page($page, $limit)
            ->order($this->sort)
            ->select()
            ->toArray();
        $userIds = array_column($list, 'user_id');
        $sum     = MallOrder::where([['uid', 'in', $userIds]])
            ->field('count(1) as count,SUM(order_price) as total_price,uid')
            ->group('uid')
            ->select()
            ->toArray();
        $sum     = array_column($sum, null, 'uid');
        foreach ($list as $key => $value) {
            $list[ $key ][ 'invite_num' ] = 0;
            $value[ 'invite_price' ]      = 0;
            foreach ($sum as $v) {
                if ($value[ 'user_id' ] == $v[ 'uid' ] && $value[ 'collage_id' ] == $v[ 'collage_id' ]) {
                    $list[ $key ][ 'invite_num' ] = $v[ 'count' ];
                    $value[ 'invite_price' ]      = $v[ 'total_price' ];
                }
            }
            $list[ $key ][ 'user_name' ]  = $value[ 'user' ][ 'name' ] ?? '';
            $list[ $key ][ 'goods_name' ] = $value[ 'goods' ][ 'title' ] ?? '';
            switch ($value[ 'collage_status' ]) {
                case 1:
                    $list[ $key ][ 'collage_status' ] = '拼团进行中';
                    break;
                case 2:
                    $list[ $key ][ 'collage_status' ] = '拼团成功';
                    break;
                case 3:
                    $list[ $key ][ 'collage_status' ] = '拼团失败';
                    break;
            }
            $list[ $key ][ 'order_price' ]         = '￥' . $value[ 'order_price' ] / 100 ?? 0;
            $list[ $key ][ 'invite_price' ]        = '￥' . $value[ 'invite_price' ] / 100 ?? 0;
            $list[ $key ][ 'collage_create_time' ] = $value[ 'collage' ][ 'create_time' ] ?? '';
        }
        $fileName = '拼团订单' . time();
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }
}